package com.qingneng.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.qingneng.service.sdk.IWorkBudgetService;
import com.qingneng.vo.BudgetItem;
import com.qingneng.vo.BudgetWorkDoInfo;
import com.qingneng.vo.BudgetWorkQuantities;
import com.tbfs.common.BaseAction;
import com.tbfs.util.UtilTool;

public class LoadBudgetItem_gdstAction extends BaseAction{
	
	private static final Logger logger = Logger.getLogger(LoadBudgetItem_gdstAction.class);
	
	private IWorkBudgetService workBudgetService = null;
	
	public String rowlist  =  null;
	
	public String getRowlist() {
		return rowlist;
	}

	public void setRowlist(String rowlist) {
		this.rowlist = rowlist;
	}

	public LoadBudgetItem_gdstAction(){
		
	}
	
	public LoadBudgetItem_gdstAction(IWorkBudgetService workBudgetService){
		this.workBudgetService = workBudgetService;
	}
	
	
	
	private int project_workinfo_id;

	
	
	
	public int getProject_workinfo_id() {
		return project_workinfo_id;
	}

	public void setProject_workinfo_id(int project_workinfo_id) {
		this.project_workinfo_id = project_workinfo_id;
	}

	

	private int parent_item_id;
	
	
	

	public int getParent_item_id() {
		return parent_item_id;
	}

	public void setParent_item_id(int parent_item_id) {
		this.parent_item_id = parent_item_id;
	}



	private List rows = null;
	


	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	private Integer total;
	
	public String loadgdstBudgetDoneItem(){
		
		if(logger.isDebugEnabled()){
			logger.debug("+++ begin execute LoadBudgetItem_gdstAction menthod +++ ");
		}
		
		logger.info("parent_item_id:"+parent_item_id);
		logger.info("project_workinfo_id:"+project_workinfo_id);
	
		
		//从模板中获取子预算项实体
		//BudgetItem budgetItem = new BudgetItem();
		//budgetItem.setParent_item_id(parent_item_id);
		//List subBudgetItem = workBudgetService.getBudgetItem(budgetItem);
		
		//获取子预算项
		BudgetWorkDoInfo bean  = new BudgetWorkDoInfo();
		bean.setParent_item_id(parent_item_id);
		bean.setProject_workinfo_id(project_workinfo_id);
		List gdstItemList = workBudgetService.getWorkDoInfo(bean);
		//保存了gdst的子项预算结果
		if(gdstItemList!=null && gdstItemList.size()>0){
			/*for(int i=0;i<gdstItemList.size();i++){
				BudgetWorkDoInfo record = (BudgetWorkDoInfo)gdstItemList.get(i);
				record.setSubBudgetItemList(subBudgetItem);
			}*/
			rows = gdstItemList;
			total = new Integer(1000);
		}else{
			//从模板中获取
			BudgetWorkDoInfo temp  = new BudgetWorkDoInfo();
			List templateList = new ArrayList();
			//temp.setSubBudgetItemList(subBudgetItem);		
			//{"budget_item_value":"请选择","item_unit":"m","budget_do_quantities":"0.0","item_unit_price":"0.0","totalAmount":"0.0"}
			temp.setBudget_item_name("请选择");
			temp.setBudget_item_value("请选择");
			temp.setItem_unit("m");
			temp.setBudget_do_quantities("-");
			temp.setTotalAmount(0.0d);
			templateList.add(temp);
			rows = templateList;
			total = new Integer(1000);
		}
		
		
		if(logger.isDebugEnabled()){
			logger.debug("+++ end execute LoadBudgetItem_gdstAction menthod +++ ");
		}
		
		return Action.SUCCESS;
	}
	
	
public String loadhfcwhDateBudgetDoneItem(){
		
		if(logger.isDebugEnabled()){
			logger.debug("+++ begin execute LoadBudgetItem_gdstAction menthod +++ ");
		}
		
		logger.info("parent_item_id:"+parent_item_id);
		logger.info("project_workinfo_id:"+project_workinfo_id);
		
		//获取子预算项
		BudgetWorkDoInfo bean  = new BudgetWorkDoInfo();
		bean.setParent_item_id(parent_item_id);
		bean.setProject_workinfo_id(project_workinfo_id);
		List itemList = workBudgetService.getWorkDoInfo(bean);
		//保存了gdst的子项预算结果
		if(itemList!=null && itemList.size()>0){
			
			rows = itemList;
			total = new Integer(1000);
		}else{
			//从模板中获取
			BudgetWorkDoInfo temp  = new BudgetWorkDoInfo();
			List templateList = new ArrayList();
			temp.setBudget_item_name("请选择");
			temp.setBudget_item_value("请选择");
			temp.setItem_unit("年");
			temp.setBudget_do_quantities("-");
			temp.setTotalAmount(0.0d);
			templateList.add(temp);
			rows = templateList;
			total = new Integer(1000);
		}
		
		
		if(logger.isDebugEnabled()){
			logger.debug("+++ end execute LoadBudgetItem_gdstAction menthod +++ ");
		}
		
		return Action.SUCCESS;
	}
	

public String loadhfcwhtimesBudgetDoneItem(){
	
	if(logger.isDebugEnabled()){
		logger.debug("+++ begin execute LoadBudgetItem_gdstAction menthod +++ ");
	}
	
	logger.info("parent_item_id:"+parent_item_id);
	logger.info("project_workinfo_id:"+project_workinfo_id);
	
	//获取子预算项
	BudgetWorkDoInfo bean  = new BudgetWorkDoInfo();
	bean.setParent_item_id(parent_item_id);
	bean.setProject_workinfo_id(project_workinfo_id);
	List itemList = workBudgetService.getWorkDoInfo(bean);
	//保存了gdst的子项预算结果
	if(itemList!=null && itemList.size()>0){
		
		rows = itemList;
		total = new Integer(1000);
	}else{
		//从模板中获取
		BudgetWorkDoInfo temp  = new BudgetWorkDoInfo();
		List templateList = new ArrayList();
		temp.setBudget_item_name("请选择");
		temp.setBudget_item_value("请选择");
		temp.setItem_unit("次");
		temp.setBudget_do_quantities("-");
		temp.setTotalAmount(0.0d);
		templateList.add(temp);
		rows = templateList;
		total = new Integer(1000);
	}
	
	
	if(logger.isDebugEnabled()){
		logger.debug("+++ end execute LoadBudgetItem_gdstAction menthod +++ ");
	}
	
	return Action.SUCCESS;
}


public String loadhfcQtBudgetDoneItem(){
	
	if(logger.isDebugEnabled()){
		logger.debug("+++ begin execute LoadBudgetItem_gdstAction menthod +++ ");
	}
	
	logger.info("parent_item_id:"+parent_item_id);
	logger.info("project_workinfo_id:"+project_workinfo_id);
	
	//获取子预算项
	BudgetWorkDoInfo bean  = new BudgetWorkDoInfo();
	bean.setParent_item_id(parent_item_id);
	bean.setProject_workinfo_id(project_workinfo_id);
	List itemList = workBudgetService.getWorkDoInfo(bean);
	//保存了gdst的子项预算结果
	if(itemList!=null && itemList.size()>0){
		
		rows = itemList;
		total = new Integer(1000);
	}else{
		//从模板中获取
		BudgetWorkQuantities record = new BudgetWorkQuantities();
		record.setProject_workinfo_id(project_workinfo_id);
		List workQuantities = workBudgetService.getWorkQuantities(record);
		BudgetWorkQuantities bw = null;
		if(workQuantities!=null && workQuantities.size()>0){
			 bw =  (BudgetWorkQuantities)workQuantities.get(0);
		}
		
		BudgetWorkDoInfo temp  = new BudgetWorkDoInfo();
		List templateList = new ArrayList();
		temp.setBudget_item_name("请选择");
		temp.setBudget_item_value("请选择");
		temp.setItem_unit("m3");
		if(bw!=null){
			temp.setBudget_do_quantities(bw.getAvgCapacity());
		}else{
			temp.setBudget_do_quantities("-");
		}
		
		temp.setTotalAmount(0.0d);
		templateList.add(temp);
		rows = templateList;
		total = new Integer(1000);
	}
	
	
	if(logger.isDebugEnabled()){
		logger.debug("+++ end execute LoadBudgetItem_gdstAction menthod +++ ");
	}
	
	return Action.SUCCESS;
}

	
	
	public String loadSubBudgetItem(){
		
		if(logger.isDebugEnabled()){
			logger.debug("+++ begin execute LoadBudgetItem_gdstAction' menthod loadSubBudgetItem_gsdt +++ ");
		}
		
		logger.info("parent_item_id:"+parent_item_id);

		//从模板中获取子预算项实体
		BudgetItem budgetItem = new BudgetItem();
		budgetItem.setParent_item_id(parent_item_id);
		List subBudgetItem = workBudgetService.getBudgetItem(budgetItem);
		rows = subBudgetItem;
		//total = new Integer(1000);
		
		
		if(logger.isDebugEnabled()){
			logger.debug("+++ end execute LoadBudgetItem_gdstAction' menthod loadSubBudgetItem_gsdt +++ ");
		}
		
		return Action.SUCCESS;
	}
	
	
	public String saveSubBudgetItem(){
		
		if(logger.isDebugEnabled()){
			logger.debug("+++ begin execute LoadBudgetItem_gdstAction' menthod saveSubBudgetItem_gsdt +++ ");
		}
		
		logger.info("parent_item_id:"+parent_item_id);

		//从模板中获取子预算项实体
		//获取子预算项
		BudgetWorkDoInfo parambean  = new BudgetWorkDoInfo();
		parambean.setParent_item_id(parent_item_id);
		parambean.setProject_workinfo_id(project_workinfo_id);
		//先删除
		workBudgetService.destroySubBudgetWorkDoInfo(parambean);
		
		
		//进行新增
		String[] rows = rowlist.split("\\|");
		if(rows!=null && rows.length>0){
			for(int i=0;i<rows.length;i++){
				String budgetInfo_jsonString = rows[i];
				logger.info("budgetInfo_jsonString:"+budgetInfo_jsonString);
				BudgetWorkDoInfo  bean =  (BudgetWorkDoInfo)UtilTool.parseJsonStr2Obj(budgetInfo_jsonString,BudgetWorkDoInfo.class);
				//JSONObject jo = JSONObject.fromObject(budgetInfo_jsonString);// 格式化成json对象
				bean.setProject_workinfo_id(project_workinfo_id);
				bean.setParent_item_id(parent_item_id);
				workBudgetService.addBudgetWorkDoInfo(bean);
			}
		}
				
		
		if(logger.isDebugEnabled()){
			logger.debug("+++ end execute LoadBudgetItem_gdstAction' menthod saveSubBudgetItem_gsdt +++ ");
		}
		
		return Action.SUCCESS;
	}
	
	
	
	
	/**
	 * 计算总价格
	 * @param dj
	 * @param work_gzl
	 * @return
	 */
	public String calTotal(int dj,String work_gzl){
		Double d_total = new Double (0.000);
		if(work_gzl!=null && !"".equals(work_gzl)){
			double d_work_gzl = new Double(work_gzl);	
			d_total = new Double (dj * d_work_gzl);
		}

		return d_total.toString();	
	}
	
}
